package com.sean.example.activity;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.sean.example.R;

public class AsyncTaskActivity extends Activity implements OnClickListener {
	
	public static final String TAG = "AsyncTaskActivity";
	
	private Button mBtn;
	private MyAsyncTask mTask;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mBtn = (Button)this.findViewById(R.id.btn);
        mBtn.setOnClickListener(this);
        
        mTask = new MyAsyncTask();
        mTask.execute(new Integer(1));
        
    }
    
    @Override
	public void onClick(View v) {
    	Log.i(TAG, "Cancel : " + mTask.cancel(true));
	}
    
    public class MyAsyncTask extends AsyncTask<Integer, Long, String> {

		@Override
		protected String doInBackground(Integer... params) {
			for(int i = 0; i < 4000; i ++) {
				publishProgress(new Long(i));
			}
			return "SUCCESS";
		}

		@Override
		protected void onPreExecute() {
			Log.i(TAG, "onPreExecute");
			super.onPreExecute();
		}

		@Override
		protected void onPostExecute(String result) {
			Log.i(TAG, "onPostExecute >> result : " + result);
			super.onPostExecute(result);
		}

		@Override
		protected void onProgressUpdate(Long... values) {
			Log.i(TAG, "onProgressUpdate >> values : " + values[0].intValue());
			super.onProgressUpdate(values);
		}

		@Override
		protected void onCancelled() {
			Log.i(TAG, "onCancelled");
			super.onCancelled();
		}
    	
		
    }

}